#include <iostream>
#include <cstdlib>
#include <ctime>
#include "graphs_VideoRenderer.h"
#include "SDL_thread.h"

#pragma warning

using namespace std; 
using namespace graphs;
using namespace gen;

int doshit (void*);

graphs::VideoRenderer *vr;

int cnt = 0,
	w = 640,
	h = 480;

short x0 = 1,
	  y0 = 1,
	  x1 = 1,
	  y1 = h;

gen::_RGBComponent *rgbc = new gen::_RGBComponent(255, 0, 0, 0);

uint32_t *texs = new uint32_t[h];


int main (int argc, char* args[])
{
	srand(time(NULL));	

	try
	{
		vr = new graphs::VideoRenderer(w, h, false, graphs::_8BITS);
	}
	catch(graphs::RenderingException ex)
	{
		cout << ex.what() << endl;
		SDL_Quit();

		return 0;
	}

	memset(texs, SDL_MapRGBA(vr->scrn->format, rgbc->r, rgbc->g, rgbc->b, rgbc->a), sizeof(uint32_t));

	SDL_Thread *thd;
	thd = SDL_CreateThread(doshit, NULL);

	SDL_WaitThread(thd, NULL);

	delete vr;
	return 0;
}

int gen_point (const int &max)
{
	int num = rand() % max + 1;
	return (num > max) ? max - 1 : num;
}

int doshit (void* nah)
{
	while(++cnt < 4000)
	{
		/*x0 = gen_point(w - 1);
		y0 = gen_point(h - 1);
		x1 = gen_point(w - 2);
		y1 = gen_point(h - 2);

		x1 = (x1 == x0) ? abs(x1 - 1) : x1;
		y1 = (y1 == y0) ? abs(y1 - 1) : y1;*/

		try
		{
			vr->Lock();
			/*vr->DrawLine(x0, y0, x1, y1, gen::_RGBComponent(abs(rand() % 255 + 1),
															abs(rand() % 255 + 1), 
															abs(rand() % 255 + 1), 0));*/
			vr->DrawVLine (x0, y0, y1, texs);

			vr->Unlock();
		}
		catch(graphs::RenderingException ex)
		{
			cout << ex.what() << endl;
		}

		vr->Refresh();
		SDL_Delay(50);

		x0++;
	}

	return 0;
}